package com.itheima.manager.service;

import com.itheima.manager.dao.BaseStudentDao;
import com.itheima.manager.dao.StudentDao;
import com.itheima.manager.domain.Student;
import com.itheima.manager.factory.StudentDaoFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class StudentService {

    private BaseStudentDao stuDao = StudentDaoFactory.getStudent();

    public void addStudent(Student stu) {

        stuDao.addStudent(stu);

    }

    public ArrayList<Student> findAllStudent() {

        ArrayList<Student> list = stuDao.findAllStudent();

        if (list.size()==0){return null;}
        else{
            Collections.sort(list, new Comparator<Student>() {
                @Override
                public int compare(Student o1, Student o2) {
                    return o1.getId().compareTo(o2.getId());
                }
            });
            return list;
        }

        /*Collections.sort(stuList, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return 0;
            }
        });*/
    }

    public boolean deleteStudent(String sSid) {

        return stuDao.deleteStudent(sSid);
    }

    //修改学生方法
    public void updateStudent(String updateId, Student newStu) {
        stuDao.updateStudent(updateId,newStu);
    }

    //校验学生学号方法
    public boolean isExists(String id) {
        int index = stuDao.getIndex(id);
        return index!=-1;
    }
}
